package cn.com.taiji;//package cn.com.taiji.oospub;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class mybatisPlus {
    private static String scanner() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + "表名,多个英文逗号分割" + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + "表名,多个英文逗号分割" + "！");
    }

    public static void main(String[] args) {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");

        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setFileOverride(true);
        gc.setAuthor("");
        gc.setOpen(false);
        gc.setIdType(IdType.ASSIGN_UUID);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
//        dsc.setDbType(DbType.SQL_SERVER);
        dsc.setDbType(DbType.MYSQL);
        //自定义数据类型转换
        dsc.setTypeConvert(new MySqlTypeConvert());
        dsc.setUrl("jdbc:mysql://192.168.164.35:3306/qadb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("90-=op[]");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName("wechat");
        pc.setParent("cn.com.taiji.pos");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing

            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/resources/mapper/pos" /*+ pc.getModuleName()*/
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null).setController(""));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //去除数据库表前缀
        strategy.setTablePrefix("sys_", "or_");
        // 表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("com.liusong.miaojob.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setSuperServiceClass("com.github.dreamyoung.mprelation.IService");
        strategy.setSuperServiceImplClass("com.github.dreamyoung.mprelation.ServiceImpl");

//        strategy.setInclude(scanner("表名"));

        String tableNames =
                "card_account\n" +
                        "card_account_type\n" +
                        "card_consumption_rule\n" +
                        "card_customer\n" +
                        "card_customer_blacklist\n" +
                        "card_customer_type\n" +
                        "card_info\n" +
                        "config_business_type\n" +
                        "config_meal_time\n" +
                        "config_quota_consumption\n" +
                        "front_hand_over\n" +
                        "front_to_work\n" +
                        "menu_food_category\n" +
                        "menu_food_item\n" +
                        "menu_food_price\n" +
                        "system_acl_empowerstrike\n" +
                        "system_acl_user\n" +
                        "trade_account_consumption_flow\n" +
                        "trade_account_store_flow\n" +
                        "trade_consumption_detail\n" +
                        "trade_consumption_main";
//        System.out.println(tableNames.replaceAll("\n",","));

        strategy.setInclude(scanner().split(","));

        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
